#include "server.h"

int taskQueueInit(taskQueue_t* taskQueue){
    bzero(taskQueue, sizeof(taskQueue_t));
    return 0;
}

int enQueue(taskQueue_t* taskQueue, int netfd){
    node_t* newNode = (node_t *)calloc(1,sizeof(node_t));
    ERROR_CHECK(newNode, NULL, "calloc failed in enQueue.\n");
    newNode->netfd = netfd;
    if(taskQueue->size == 0){
        taskQueue->head = newNode;
        taskQueue->tail = newNode;
    }else{
        taskQueue->tail->next = newNode;
        taskQueue->tail = newNode;
    }
    ++taskQueue->size;
    return 0;
}

int deQueue(taskQueue_t* taskQueue){
    if(taskQueue->size <= 0){
        return -1;
    }
    node_t* tmp = taskQueue->head;
    taskQueue->head = tmp->next;
    if(taskQueue->size == 1){
        taskQueue->tail = NULL;
    }
    free(tmp);
    --taskQueue->size;
    return 0;
}

int printQueue(taskQueue_t* taskQueue){
    node_t* cur = taskQueue->head;
    while(cur){
        printf("%3d", cur->netfd);
        cur = cur->next;
    }
    printf("\n");
    return 0;
}

//int main(){
//    taskQueue_t taskQueue;
//    taskQueueInit(&taskQueue);
//    for (int i = 0; i < 10; ++i){
//        enQueue(&taskQueue, i);
//        printQueue(&taskQueue);
//    }
//    for(int i = 0; i < 10; ++i){
//        deQueue(&taskQueue);
//        printQueue(&taskQueue);
//    }
//    return 0;
//}
